/**
 * Created by liuqingling on 16/6/12.
 */
define(function(){
    var Bridge = function(options){
        this.data = {};
        this.fnData = '';
        this.types = ['selectmember', 'datepicker', 'dudu', 'file', 'selectmenu', 'city', 'map', 'reply', 'viewimage', 'mail', 'message', 'calluser', 'group', 'viewuser', 'openchatwindow', 'copytext', 'speech', 'previewfile', 'at', 'viewdeleteimage', 'selectdepartment'];
        this.clickEvent = 'tap';
        this.setting = $.extend(true, {
            bridge : '',
            element : '',
            type : '',
            data : '',
            debug : false,
            before : '',
            callback : ''
        }, options || {});
        this.init();
    };
    Bridge.prototype = {
        init : function(){
            var _self = this, setting = _self.setting, type = setting.type.toLowerCase();
            if($.inArray(type, _self.types) >= 0){
                _self.send();
            }
        },
        selectmember : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectContacts', 'parameters':_self.data};
        },
        datepicker : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectDate', 'parameters':_self.data};
        },
        dudu : function(){
            var _self = this, setting = _self.setting;
            return {'function':'callUer', 'parameters':_self.data};
        },
        calluser : function(){
            var _self = this, setting = _self.setting;
            return {'function':'callUsers', 'parameters':_self.data};
        },
        file : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectAttachment', 'parameters':_self.data};
        },
        selectmenu : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectList', 'parameters':_self.data};
        },
        city : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectCity', 'parameters':_self.data};
        },
        map : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectMap', 'parameters':_self.data};
        },
        reply : function(){
            var _self = this, setting = _self.setting;
            return {'function':'feedReply', 'parameters':_self.data};
        },
        mail : function(){
            var _self = this, setting = _self.setting;
            return {'function':'sendMemail', 'parameters':_self.data};
        },
        viewimage : function(){
            var _self = this, setting = _self.setting;
            return {'function':'viewImage', 'parameters':_self.data};
        },
        message : function(){
            var _self = this, setting = _self.setting;
            return {'function':'sendMessage', 'parameters':_self.data};
        },
        group : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectGroup', 'parameters':_self.data};
        },
        viewuser : function(){
            var _self = this, setting = _self.setting;
            return {'function':'viewUser', 'parameters':_self.data};
        },
        openchatwindow : function(){
            var _self = this, setting = _self.setting;
            return {'function':'openChatWindow', 'parameters':_self.data};
        },
        copytext : function(){
            var _self = this, setting = _self.setting;
            return {'function':'copyText', 'parameters':_self.data};
        },
        speech : function(){
            var _self = this, setting = _self.setting;
            return {'function':'createFeed', 'parameters':_self.data};
        },
        previewfile : function(){
            var _self = this, setting = _self.setting;
            return {'function':'previewFile', 'parameters':_self.data};
        },
        at : function(){
            var _self = this, setting = _self.setting;
            return {'function':'processAt', 'parameters':_self.data};
        },
        selectdepartment : function(){
            var _self = this, setting = _self.setting;
            return {'function':'selectDepartmentForVerify', 'parameters':_self.data};
        },
        viewdeleteimage : function(){
            var _self = this, setting = _self.setting;
            return {'function':'viewOrDeleteImage', 'parameters':_self.data};
        },
        initData : function(ev){
            var _self = this, setting = _self.setting, fnData = setting.data;
            if(typeof fnData === 'function'){
                _self.data = fnData(ev);
            } else{
                _self.data = fnData;
            }
            if(typeof _self.data === 'string'){
                _self.data = JSON.parse(_self.data);
            }
        },
        getData : function(ev){
            var _self = this, setting = _self.setting, type = setting.type.toLowerCase();
            _self.initData(ev);
            _self.fnData = $(ev.currentTarget).attr('data-bridge') || '{}';
            if(typeof _self.fnData === 'string'){
                _self.fnData = JSON.parse(_self.fnData);
            }
            _self.data = $.extend({}, _self.data, _self.fnData);
            data = _self[type]();
            if(typeof data === 'object'){
                try{
                    data = JSON.stringify(data);
                } catch(e){

                }
            }
            return data;
        }
    };
    return Bridge;
});


